package com.example.securitydemo.service.impl;

import com.example.securitydemo.config.AppProperties;
import com.example.securitydemo.service.EmailService;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "mooc.email-provider", name = "name", havingValue = "smtp")
public class EmailServiceSmtpImpl implements EmailService {

    private final JavaMailSender mailSender;
    private final AppProperties appProperties;

    @Override
    public void send(String email, String msg) {
        val message = new SimpleMailMessage();
        message.setTo(email);
        message.setCc(appProperties.getEmail().getUsername());
        message.setFrom(appProperties.getEmail().getUsername());
        message.setSubject("登录验证码");
        message.setText("哎，这是我给你的验证码呢，请不要告诉别人验证码是：" + msg);
        mailSender.send(message);
    }
}
